package edu.xust.MetadataGenerator.domain;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.annotations.Expose;

public class Field implements Serializable {
	private static final long serialVersionUID = 1L;

	private static final long id = Table.generateId();

	@Expose
	private String name;
	
	@Expose
	private String label;
	
	@Expose 
	private String placeholder;
	
	@Expose 
	private String unit;
	
	@Expose
	private String comment;

	@Expose
	private String type;
	
	@Expose
	private String def;
	
	@Expose
	private List<FieldConstraint> constraints;

	@Expose
	private List<Option> options;
	
	@Expose
	private String remoteURL;
	
	@Expose
	private int map_type;	//映射类型,0为非映射，1为1:1，2为1:N
	
	@Expose
	private String map_class;	//映射对象类型
	
	@Expose
	private List<FieldValidator> validators;

	public Field(String name){
		this.name = name;
		type = "int";
		label = "label";
		constraints = new LinkedList<FieldConstraint>();
		validators = new LinkedList<FieldValidator>();
	}
	
	 public Object deepClone(){
		 Object res = null;
		 
		 //将对象写到流里   
		 ByteArrayOutputStream bo=new ByteArrayOutputStream();   
		 ObjectOutputStream oo;
		 try {
			 oo = new ObjectOutputStream(bo);
			 oo.writeObject(this);   
			 //从流里读出来   
			 ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());   
			 ObjectInputStream oi=new ObjectInputStream(bi);  
			 res = oi.readObject();
		 } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		 }   
		 return res;   
	 }   
	public Field(){
		name = "myfield";
		type = "int";
		label = "label";
		constraints = new LinkedList<FieldConstraint>();
		validators = new LinkedList<FieldValidator>();
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name.replace(" ", "");
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type.replace(" ", "");
	}
	public List<FieldConstraint> getConstraints() {
		return constraints;
	}
	public void setConstraints(List<FieldConstraint> constraints) {
		this.constraints = constraints;
	}
	public List<FieldValidator> getValidators() {
		return validators;
	}
	public void setValidators(List<FieldValidator> validators) {
		this.validators = validators;
	}
	public static long getId() {
		return id;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return name;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public List<Option> getOptions() {
		return options;
	}

	public void setOptions(List<Option> options) {
		this.options = options;
	}

	public String getJname() {
		String jname = name;
		if(jname.startsWith("_")){
			jname = jname.substring(1);
		}
		jname = jname.replace(" " , "");
		jname = jname.replace(".", "_");
		jname = jname.toLowerCase();
		return jname;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getDef() {
		return def;
	}

	public void setDef(String def) {
		this.def = def;
	}

	public String getRemoteURL() {
		return remoteURL;
	}

	public void setRemoteURL(String remoteURL) {
		this.remoteURL = remoteURL;
	}
	
	public String getConstraints_en(){
		String en = "";
		if(null != this.constraints){
			for(FieldConstraint fc : this.constraints){
				en += fc.getName() + ",";
			}
			int lastComma = en.lastIndexOf(",");
			if((-1 != lastComma) && (lastComma == (en.length() - 1))){
				en = en.substring(0, en.length() - 1);
			}
		}
		return en;
	}

	public int getMap_type() {
		return map_type;
	}

	public void setMap_type(int map_type) {
		this.map_type = map_type;
	}

	public String getMap_class() {
		return map_class;
	}

	public void setMap_class(String map_class) {
		this.map_class = map_class;
	}
}
